package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.infra.InfraApi;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.DataGroupSaveReq;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.DataGroupUpdateReq;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.GroupCodeValidation;
import cn.lingyangwl.agile.model.enums.DataGroupBizTypeEnum;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.system.service.SysConfigService;
import cn.lingyangwl.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 配置组
 *
 * @author shenguangyang
 */
@Api(tags = "配置组")
@RestController
@RequestMapping("/config/group")
public class SysConfigGroupController {
    @Resource
    private SysConfigService configService;
    @Resource
    private InfraApi infraApi;

    @ApiOperation(value = "保存配置组")
    @PostMapping("/save")
    @RamResource(action = "sys:configGroup:save", name = "保存配置组", type = ActionType.WRITE)
    public Resp<?> saveDataGroup(@RequestBody @Validated(GroupCodeValidation.class) DataGroupSaveReq req) {
        req.setBizType(DataGroupBizTypeEnum.CONFIG.getType());
        return Resp.ok(infraApi.saveDataGroup(req));
    }

    @ApiOperation(value = "更新配置组")
    @PostMapping("/update")
    @RamResource(action = "sys:configGroup:update", name = "更新配置组", type = ActionType.WRITE)
    public Resp<?> updateDataGroup(@RequestBody @Validated(GroupCodeValidation.class) DataGroupUpdateReq req) {
        return Resp.ok(infraApi.updateDataGroup(req));
    }

    @ApiOperation(value = "批量删除配置组")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "sys:configGroup:delete", name = "删除配置组", type = ActionType.WRITE)
    public Resp<?> batchDeleteDataGroup(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(configService.batchDeleteGroup(DataGroupBizTypeEnum.CONFIG, ids));
    }

    @ApiOperation(value = "删除配置组")
    @DeleteMapping("/delete")
    @RamResource(action = "sys:configGroup:delete", name = "删除配置组", type = ActionType.WRITE)
    public Resp<?> deleteDataGroup(@RequestParam(value = "id") Long id) {
        return Resp.ok(configService.batchDeleteGroup(DataGroupBizTypeEnum.CONFIG, Collections.singletonList(id)));
    }
}
